package org.dromara.wmscloud.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.wmscloud.domain.bo.StockInDetailBo;
import org.dromara.wmscloud.domain.vo.StockInDetailVo;
import org.dromara.wmscloud.domain.StockInDetail;
import org.dromara.wmscloud.mapper.StockInDetailMapper;
import org.dromara.wmscloud.service.IStockInDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 入库单详情Service业务层处理
 *
 * @author Lion Li
 * @date 2025-05-02
 */
@RequiredArgsConstructor
@Service
public class StockInDetailServiceImpl implements IStockInDetailService {

    private final StockInDetailMapper baseMapper;

    /**
     * 查询入库单详情
     *
     * @param detailId 主键
     * @return 入库单详情
     */
    @Override
    public StockInDetailVo queryById(Long detailId){
        return baseMapper.selectVoById(detailId);
    }

    /**
     * 分页查询入库单详情列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 入库单详情分页列表
     */
    @Override
    public TableDataInfo<StockInDetailVo> queryPageList(StockInDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StockInDetail> lqw = buildQueryWrapper(bo);
        Page<StockInDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的入库单详情列表
     *
     * @param bo 查询条件
     * @return 入库单详情列表
     */
    @Override
    public List<StockInDetailVo> queryList(StockInDetailBo bo) {
        LambdaQueryWrapper<StockInDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StockInDetail> buildQueryWrapper(StockInDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StockInDetail> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(StockInDetail::getDetailId);
        lqw.eq(StringUtils.isNotBlank(bo.getWarehouseCode()), StockInDetail::getWarehouseCode, bo.getWarehouseCode());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), StockInDetail::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getLineNo() != null, StockInDetail::getLineNo, bo.getLineNo());
        lqw.eq(StringUtils.isNotBlank(bo.getMatCode()), StockInDetail::getMatCode, bo.getMatCode());
        lqw.like(StringUtils.isNotBlank(bo.getMatName()), StockInDetail::getMatName, bo.getMatName());
        lqw.eq(StringUtils.isNotBlank(bo.getFdCode()), StockInDetail::getFdCode, bo.getFdCode());
        lqw.eq(StringUtils.isNotBlank(bo.getFigNum()), StockInDetail::getFigNum, bo.getFigNum());
        lqw.eq(StringUtils.isNotBlank(bo.getMatGroup()), StockInDetail::getMatGroup, bo.getMatGroup());
        lqw.eq(StringUtils.isNotBlank(bo.getMatClass()), StockInDetail::getMatClass, bo.getMatClass());
        lqw.eq(StringUtils.isNotBlank(bo.getUnitCode()), StockInDetail::getUnitCode, bo.getUnitCode());
        lqw.eq(StringUtils.isNotBlank(bo.getLabelId()), StockInDetail::getLabelId, bo.getLabelId());
        lqw.eq(StringUtils.isNotBlank(bo.getBatch()), StockInDetail::getBatch, bo.getBatch());
        lqw.eq(bo.getQuantity() != null, StockInDetail::getQuantity, bo.getQuantity());
        lqw.eq(bo.getQualifiedQuantity() != null, StockInDetail::getQualifiedQuantity, bo.getQualifiedQuantity());
        lqw.eq(bo.getStockInQuantity() != null, StockInDetail::getStockInQuantity, bo.getStockInQuantity());
        lqw.eq(bo.getUnitPrice() != null, StockInDetail::getUnitPrice, bo.getUnitPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getSupplierCode()), StockInDetail::getSupplierCode, bo.getSupplierCode());
        lqw.like(StringUtils.isNotBlank(bo.getSupplierName()), StockInDetail::getSupplierName, bo.getSupplierName());
        lqw.eq(bo.getProdTime() != null, StockInDetail::getProdTime, bo.getProdTime());
        return lqw;
    }

    /**
     * 新增入库单详情
     *
     * @param bo 入库单详情
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(StockInDetailBo bo) {
        StockInDetail add = MapstructUtils.convert(bo, StockInDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    /**
     * 修改入库单详情
     *
     * @param bo 入库单详情
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(StockInDetailBo bo) {
        StockInDetail update = MapstructUtils.convert(bo, StockInDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StockInDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除入库单详情信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
